<template>
    <el-form :model="user" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="用户名" prop="yhm">
                    <el-input v-model="user.yhm" placeholder="用户名" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="初始密码" prop="pwd">
                    <el-input v-model="user.pwd" placeholder="密码" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="用户昵称" prop="yhnc">
                    <el-input v-model="user.yhnc" placeholder="用户昵称" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="user.sex">
                        <el-radio value="男">男</el-radio>
                        <el-radio value="女">女</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="用户身份" prop="yhsf">
                    <el-select
                            v-model="user.yhsf"
                            placeholder="请选择"
                            clearable
                    >
                        <el-option label="管理员" value="0"/>
                        <el-option label="普通管理员" value="2"/>
                        <el-option label="用户" value="3"/>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="手机号" prop="sjh">
                    <el-input v-model="user.sjh" placeholder="手机号" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="邮箱" prop="email">
            <el-input v-model="user.email" placeholder="邮箱" clearable/>
        </el-form-item>

        <el-form-item label="用户头像" prop="yhtx">
            <el-upload drag style="width:100%"
                       action="/api/file/upload"
                       :on-success="handleFileUploadSuccess"
            >
                <el-icon class="el-icon--upload"><upload-filled/></el-icon>
                <div class="el-upload__text">将文档拖拽到此处，或 <em>点击上传</em></div>
                <template #tip>
                    <div class="el-upload__tip">支持 jpg/png 格式， 大小不超过 5M</div>
                </template>
            </el-upload>
        </el-form-item>
    </el-form>

    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits(["closeDialog", "flushPage"]);

const user = ref({
    yhbh: '',
    yhm: '',
    pwd: '123456',
    yhnc: '',
    yhsf: '',
    sex: '男',
    sjh: '',
    email: '',
    yhtx: ''
})

//-文件上传处理-
const handleFileUploadSuccess = (fileName) => {
    if (fileName != null || fileName != '') {
        user.value.yhtx = fileName;
        ElMessage({type: 'success',message:'文件上传成功'})
    }else{
        ElMessage({type: 'error',message:'文件上传失败'})
    }
}

// 取消：关闭 dialog
const doCancel = () => {
    emits("closeDialog");
}

// 新增
const doSave = async () => {
    console.log(user.value);
    const {data} = await axios.post('/api/user', user.value);
    if (data > 0) {
        emits("flushPage");				// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>